<?php

require_once('includes/init.inc.php');
require_once('classes/TMSFunctions.cls.php');
require_once('classes/User.cls.php');
require_once('classes/Project.cls.php');
require_once('classes/Task.cls.php');
require_once('classes/Client.cls.php');
require_once('classes/AdminPage.cls.php');


if(!(isset($_SESSION['username'])))
{
	header('Location:index.php');
	die();
}

$username=$_SESSION['username'];
$user = new User(TMSFunctions::getIdFromUsername($username));

$admin = new AdminPage();

if(isset($_POST['clients']))
{
	echo $admin-> getClientsTable();
	die();
}


if(isset($_POST['showModules']))
{

	$pjt = $_POST['showModules'];
	echo $admin->showModuleTable($pjt);
	die();
}
if(isset($_POST['allprojects']))
{
	$project=project::getProjects();
	echo $admin->showProjectTable($project);
	die();
}
if(isset($_POST['users']))
{

	echo $admin->showUsersTable();
	die();
}
if(isset($_POST['addproject']))
{

	echo $admin->addProjectTable();
	die();
}
if(isset($_POST['addNewproject']))
{
	$projectName= $_POST['projectName'];
	$projectDesc=$_POST['projectDesc'];
	$projectClient=$_POST['projectClient'];
	$projectDeadline= $_POST['projectDeadline'];
	$r=Project::addProject($projectName,$projectDesc,$projectClient,$projectDeadline);
	$project=project::getProjects();
	echo $admin->showProjectTable($project);

	die();
}
if(isset($_POST['addUser']))
{

	echo $admin->addUserTable();
	die();
}
if(isset($_POST['addnewUser']))
{
	$fname=$_POST['firstname'];
	$lname=$_POST['lastname'];
	$username=$_POST['username'];
	$passhash=md5($_POST['password']);
	$email=$_POST['email'];
	$designation=$_POST['designation'];
	$usertype=$_POST['usertype'];
	$r=User::addUser($username,$passhash,$fname,$lname,$email,$designation,$usertype);
	echo $admin->showUsersTable();


	die();
}

if(isset($_POST['deleteProject']))
{

	$id=$_POST['deleteProject'];
	Project::deleteProject($id);
	$project=project::getProjects();
	echo $admin->showProjectTable($project);
	die();
}
if(isset($_POST['deleteUser']))
{

	$id=$_POST['deleteUser'];
	User::deleteUser($id);
	echo $admin->showUsersTable();
	die();
}


if(isset($_POST['deleteClient']))
{

	$id=$_POST['deleteClient'];
	Client::deleteClient($id);
	echo $admin-> getClientsTable();
	die();
}

if(isset($_REQUEST['getProjectName']))
{
	echo Project::getProjectNameById($_REQUEST['getProjectName']);
	die;
}

if(isset($_REQUEST['addClient']))
{
	echo $admin-> addClientTable();
	
	die;
}


if(isset($_POST['addNewClient']))
{
	$name= $_POST['name'];
	$address=$_POST['address'];
	$email=$_POST['email'];
	$number= $_POST['number'];
	$status= $_POST['status'];
	Client::addClient($name,$address,$email,$number,$status);
echo $admin->getClientsTable();
	die();
}

if(isset($_REQUEST['editProjTable']))
{
	$id=$_REQUEST['editProjTable'];
	echo $admin-> editProjectTable($id);
	
	die;
}
if(isset($_REQUEST['project']))
{
	$id=$_REQUEST['project'];
	echo $admin->project($id);
	
	die;
}

if(isset($_REQUEST['updateProjectTable']))
{
	$sorty = $_POST['sort'];
	$desc = $_POST['desc'];
	$search = $_POST['search'];
	$client =$_POST['s'];
	
		
		if($desc=="false")
		$desc=false;
	else
		$desc=true;
	
	$project=$user->project($sorty,$desc,$search,$client);
	
	echo $admin->showProjectTable($project);

	die;

}

if(isset($_POST['updateproject']))
{
	$id= $_POST['id'];
	$editProjectName= $_POST['editProjectName'];
	$editProjectDesc=$_POST['editProjectDesc'];
	$editProjectClient=$_POST['editProjectClient'];
	$editProjectDeadline= $_POST['editProjectDeadline'];
	$updateproj=new Project($id);
	$r=$updateproj->updateProject($editProjectName,$editProjectDesc,$editProjectClient,$editProjectDeadline);
	if($r==true)
	{
	$project=project::getProjects();
	echo $admin->showProjectTable($project);
	}
	else echo "no";

	die();
}

if(isset($_REQUEST['addMemberTable']))
{

	$id=$_REQUEST['addMemberTable'];
	echo $admin->addProjectMemberTable($id);
	die;
}
if(isset($_REQUEST['addProjectMember']))
{

	$id=$_REQUEST['id'];
	$userID=$_REQUEST['member'];
	$memberRole=$_REQUEST['memberRole'];
	$addmember= new Project($id);
	$addmember->addMember($userID,$memberRole);
	echo $admin->project($id);
	die;
}
if(isset($_REQUEST['addModuleTable']))
{

	$id=$_REQUEST['addModuleTable'];
	echo $admin->addProjectModuleTable($id);
	die;
}
if(isset($_REQUEST['addProjectModule']))
{

	$id=$_REQUEST['id'];
	$moduleName=$_REQUEST['module'];
	$addmodule= new Project($id);
	$addmodule->addModule($moduleName);
	echo $admin->project($id);
	die;
}
if(isset($_REQUEST['taskTable']))
{

	$moduleid=$_REQUEST['taskTable'];
	echo $admin->taskByModuleTable($moduleid);
	die;
}


$admin->showHeader($user);
$admin->mainMenu();
?>
<div class="clear"></div>


<div id="dynamic">
	<?php
		$admin->filterMenu()
	?>

	<div id="task_details"><!--Project-details-->
		<div id='tablediv' >
			<?php
				$project=project::getProjects();
				$admin->showProjectTable($project);
			?>				
		</div>
	</div><!--Project-details-->
</div>
<?php
	$admin->footer();
?>